var equipmentDetail = {};
var chargingStationMap = undefined;
var markers = [];
var selectPosition = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	addTitle("equipmentInfoDetails");
	search();
});

function search(){
	$('#EquipmentID').val($("#detailID", parent.document).val());
	$ajax('searchEquipmentDetail', 'equipmentSearch', searchEquipmentInfo);
	$("#detailID", parent.document).val("");
}

function searchEquipmentInfo(data) {
	equipmentDetail = data && data.equipmentDetail || {};
	createDetailUlBody("detailOneBody", equipmentInfoDetailTable, equipmentDetail, 1);
	createDetailUlBody("detailTwoBody", equipmentInfoDetailTable, equipmentDetail, 2);
	createDetailUlBody("detailThreeBody", equipmentInfoDetailTable, equipmentDetail, 3);
	$("#stationName").html('');
	$("#equipmentMsg").html('');
	$("#stationName").html('<a class="details fontWhite" onclick=\'chargingStationName()\'>' + (equipmentDetail.chargingStationName || '未获取到充电桩详细信息') + '</a>');
	$("#equipmentMsg").html((equipmentDetail.equipmentName || '充电桩#-') + ' <img src="../static/images/dotGreen.png" width="10" height="10"> ' + (equipmentDetail.currentType || '-')).css({fontSize: "26px"});
	$('#confirm').hide();
	$('#confirmMap').hide();
	$("#longitude").attr("readonly", true);
	$("#latitude").attr("readonly", true);
	if (!chargingStationMap) {
		$.getScript("https://webapi.amap.com/maps?v=1.3&key=244bc2cb511336b2a3e5129ad0309a13&&plugin=AMap.OverView,AMap.MapType,AMap.Geolocation,AMap.Scale,AMap.ToolBar,AMap.Autocomplete,AMap.PlaceSearch&callback=tableMap");
	} else {
		tableMap();
	}
	setTimeout(hideLoading, 500);
}
function chargingStationName(){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(equipmentDetail.chargingStationID);
}
function goback() {
	window.parent.skipMenu('chargingEquipment/equipmentInfo', 'drownDown2', '', 'equipmentPage', 'equipmentInfo');
}
function changeGPS() {
	$("#map").modal("show");
	var longitude = $("#longitude").text();
	var latitude = $("#latitude").text();
	if (longitude && latitude) {
		var position = [];
		position.push(longitude);
		position.push(latitude);
	}
	chargingStationMap = setMap(position);
	AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
		var autoOptions = {
			city: '', //城市，默认全国  
	        input: "keyword"//使用联想输入的input的id
	    };
	    autocomplete= new AMap.Autocomplete(autoOptions);
		var placeSearch = new AMap.PlaceSearch({
			city: '', //城市，默认全国  
	        map: chargingStationMap
	    });
	    AMap.event.addListener(autocomplete, "select", function(e){
	        placeSearch.search(e.poi.name);
	    });
	});
	AMap.event.addListener(chargingStationMap, 'click', getLnglat);
	addMarker(position, "static/images/chargingStationMap.png");
}
function getLnglat(e){
	if(deleteFlag != "details"){
		selectPosition = [];
		selectPosition.push(e && e.lnglat && e.lnglat.getLng() || 0);
		selectPosition.push(e && e.lnglat && e.lnglat.getLat() || 0);
		addMarker(selectPosition, "static/images/chargingStationMap.png");
	}
}
function tableMap(){
	var longitude = $("#longitude").text();
	var latitude = $("#latitude").text();
	if (longitude && latitude) {
		var centerPosition = [];
		centerPosition.push(longitude);
		centerPosition.push(Number(latitude) + 0.015);
		var position = [];
		position.push(longitude);
		position.push(latitude);
	}
	chargingStationMap = setMap(centerPosition, 'tableContainer', 10);
	addMarker(position, "static/images/chargingStationMapsm.png");
}
function confirmMap(){
	$("#map").modal("hide");
	if (selectPosition && selectPosition.length > 0) {
		$("#longitude").text(selectPosition[0]);
		$("#latitude").text(selectPosition[1]);
	}
	tableMap();
}
function addMarker(position, icon){
	if (position && position.length > 0) {
		chargingStationMap.remove(markers);
		markers = [];
		marker = new AMap.Marker({
			icon: icon,
	        map: chargingStationMap,
	        position: position,
	        animation: "AMAP_ANIMATION_DROP"
	    });
		markers.push(marker);
		marker = null;
	}
}
